$(function() {
        	
	var service = new EbeimService();
	var op = $(".form-group");
	var form = $("#j-form");
	var phone = $("#j-phone");
	phone.text(GetQueryString("phone"));
	op.each(function() {
        var t = $(this),
            input = t.find(".required"),
            name = input.attr("name"),
            holder = input.siblings(".message");
        input.blur(function() {
            activeCheck(form,name);
        }).focus(function() {
            holder.show();
        });;
    });
	form.delegate(".j-getCode","click",function(){
		var data = {
			token:"tokennawang",
			username: GetQueryString("username")
			
		}
		var t = $(this);
    	t.attr("disabled", "disabled").removeClass("codeBtn").addClass("disableBtn");
    	countDown(t,120);
    	service.getMessageCode(data,function(json){
			if(json.code == 200) {
				alert(json.desc);
			}else{
                alert(json.desc);
            }
		})	
    });
	form.delegate("#j-submit","click",function () {
        if (!activeCheck(form)) {
        	return false;
        }else{
            var data = form.serialize();
            
            data = data + "&userId="+ GetQueryString("userId") +"&token=tokennawang";


            service.oncePasswordUpdate(data, function(json){
                if(json.code == 200) {
                    alert(json.desc);
                    window.location.href = "../login.html";
                }else{
                	alert(json.desc);
                }
            });
        }
    })
});


//激活验证
var activeCheck = function(form,name) {

	// 显示错误信息
    var showError = function(obj, msg) {
        var msgDom = obj.siblings('.message');
        var msgTxt = msgDom.children('i')
        msgDom.addClass("msg-error").removeClass('msg-tip');
        msgTxt.html(msg);
        msgDom.show();
    };

    // 隐藏错误信息
    var hideError = function(obj, msg) {
        var msgDom = obj.siblings('.message');
        var msgTxt = msgDom.children('i')
        msgDom.hide();
        msgDom.addClass("msg-tip").removeClass('msg-error');
        msgTxt.html(msg);
    };

    // 数据验证对象
    var dataCheck = {
        newPassword:{
            obj: form.find("[name='newPassword']"),
            fn: function() {
                var val = this.obj.val();
                if (formCheck.isNull(val)) {
                    showError(this.obj,"请输入新密码");
                    return false;
                }else if(!YBA.Common.check.isPwd(val)){
                	showError(this.obj,"至少六位的英文数字组合");
                    return false;
                }else if(formCheck.isHowLong(val,50)){
                	showError(this.obj,"新密码过长");
                    return false;
                } else {
                    hideError(this.obj,"请输入新密码");
                    return true;
                }
            }
        },
        passwordSecond: {
            obj: form.find("[name='passwordSecond']"),
            fn: function() {
                var val = this.obj.val();
                if (formCheck.isNull(val)) {
                    showError(this.obj,"请再次输入新密码");
                    return false;
                } else if(val != dataCheck["newPassword"].obj.val()){
                    showError(this.obj,"两次输入的密码不一致");
                    return false;
                }else {
                    hideError(this.obj,"请再次输入新密码");
                    return true;
                }
            }
        },
        oldPassword: {
            obj: form.find("[name='oldPassword']"),
            fn: function() {
                var val = this.obj.val();
                if (formCheck.isNull(val)) {
                    showError(this.obj,"请输入旧密码");
                    return false;
                } else if(formCheck.isHowLong(val,50)){
                	showError(this.obj,"旧密码过长");
                    return false;
                } else {
                    hideError(this.obj,"请输入旧密码");
                    return true;
                }
            }
        },
        code: {
            obj: form.find("[name='code']"),
            fn: function() {
            	if(this.obj.length == 0){
            		return true;
            	}
                var val = this.obj.val();
                if (formCheck.isNull(val)) {
                    showError(this.obj,"请输入验证码");
                    return false;
                } else {
                    hideError(this.obj,"请输入验证码");
                    return true;
                }
            }
        }
    };

    if (name && dataCheck[name]) {
        return dataCheck[name].fn();
    } else {
        var errLen = 0;
        for (var i in dataCheck) {
            if (!dataCheck[i].fn()) {
                errLen++;
            }
        }
        if (errLen > 0) {
            return false;
        }
        return true;
    }
}
//记录定时器
var timer = "";
var countDown = function(dom,time){
	dom.val(time+"秒");
	timer = setInterval(function(){
		if(time>0){
			time--;
    		dom.val(time+"秒");
		}else{
			clearInterval(timer);
			dom.removeAttr("disabled").removeClass("disableBtn").addClass("codeBtn").val("发送验证码");
		}
	},1000);
}